import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import StorageUtils from '../../../app/storage';
import update from 'react-addons-update';
import DB from '../../../app/db';


export default Reflux.createStore({
    listenables: [Actions],
    data: {
        ...PageConst,
      txmap : new Map(),
      currentTx:{},
      currentJL:{},
      items: [1,2,3,4,5,6,7,8,9],
      userInfo: {
        certNum: '',
        telNum: '',
        vcode: '',
        name:'',
        organName:''
      },
      qrInfo:{},
    },

  onLoadAQZG: function (model) {
    let t = this;
    this.onAQZGFetch({
      page: 1,
      tel:'13972608122',
      jllx:'0022',
      size: 1,
    });

  },



  onLoadZLZG: function (model) {
    let t = this;
    this.onZLZGFetch({
      page: 1,
      tel:'13972608122',
      jllx:'0008',
      size: 1,
    });

  },

  onLoadZLTG: function (model) {
    let t = this;

    this.onZLTGFetch({
      page: 1,
      tel:'13972608122',
      jllx:'0009',
      size: 1,
    });

  },

  onLoadAQTG: function (model) {
    let t = this;
    this.onAQTGFetch({
      page: 1,
      tel:'13972608122',
      jllx:'0023',
      size: 1,
    });

  },

  onLoadAQJB: function (model) {
    let t = this;
    this.onAQJBFetch({
      page: 1,
      tel:'13972608122',
      jllx:'0052',
      size: 1,
    });

  },

  onAQJBFetch: function (params,obj) {
    let t = this;
    if (!__LOCAL__) {
      params.tel = this.data.userInfo.telNum;
    }
    DB.WechatAPI.findJLDLByLxControl(params).then(
      function (content) {
        if (null != content  ) {
          if (content.resultls.length > 0) {
            t.data.aqjb = update(t.data.aqjb, {relObj: {$set: content.resultls[0]}});
            t.data.aqjb = update(t.data.aqjb, {reljl: {$set: content.resultls}});
            t.data.txmap.set("安全隐患局部停工整改通知书", t.data.aqjb);
          }
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
    console.info("安全局部");
  },

  onAQTGFetch: function (params,obj) {
    let t = this;
    if (!__LOCAL__) {
      params.tel = this.data.userInfo.telNum;
    }
    DB.WechatAPI.findJLDLByLxControl(params).then(
      function (content) {
        if (null != content) {
          if (content.resultls.length > 0) {
            t.data.aqtg = update(t.data.aqtg, {relObj: {$set: content.resultls[0]}});
            t.data.aqtg = update(t.data.aqtg, {reljl: {$set: content.resultls}});
            t.data.txmap.set("安全隐患责令停工通知书", t.data.aqtg);
          }
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
    console.info("安全停工");
  },

  onZLTGFetch: function (params,obj) {
    let t = this;
    if (!__LOCAL__) {
      params.tel = this.data.userInfo.telNum;
    }
    DB.WechatAPI.findJLDLByLxControl(params).then(
      function (content) {
        if (null != content  ) {
          if (content.resultls.length > 0) {
            t.data.zltg = update(t.data.zltg, {relObj: {$set: content.resultls[0]}});

            t.data.zltg = update(t.data.zltg, {reljl: {$set: content.resultls}});
            t.data.txmap.set("质量局部暂停施工通知书", t.data.zltg);
          }
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
    console.info("质量停工");
  },

  onZLZGFetch: function (params,obj) {
    let t = this;


    if (!__LOCAL__) {
      params.tel = this.data.userInfo.telNum;
    }
    console.info("质量整改",params.tel);
    DB.WechatAPI.findJLDLByLxControl(params).then(
      function (content) {
        if (null != content ) {
          if (content.resultls.length > 0) {
            t.data.zlzg = update(t.data.zlzg, {relObj: {$set: content.resultls[0]}});
            t.data.zlzg = update(t.data.zlzg, {reljl: {$set: content.resultls}});
            t.data.txmap.set("质量问题整改通知书", t.data.zlzg);
          }
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });

  },

  onAQZGFetch: function (params) {
    let t = this;
    if (!__LOCAL__) {
      params.tel = this.data.userInfo.telNum;
    }
    console.info("联系", params.tel);
    console.info("变量",!__LOCAL__);
    DB.WechatAPI.findJLDLByLxControl(params).then(
      function (content) {
        if (null != content &&  null != content.resultls ) {
          if (content.resultls.length > 0) {

            t.data.aqzg = update(t.data.aqzg, {relObj: {$set: content.resultls[0]}});
            t.data.aqzg = update(t.data.aqzg, {reljl: {$set: content.resultls}});
            t.data.txmap.set("安全隐患限期整改通知书", t.data.aqzg);
          }
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
    console.info("安全整改");
  },



  initJDJL: function () {

    Actions.loadZLZG(true);
    Actions.loadAQZG(true);
    Actions.loadZLTG(true);
    Actions.loadAQTG(true);
    Actions.loadAQJB(true);
  },

  onSetCurrentJL: function (obj) {
    this.data.currentJL = update(this.data.currentJL, {$set: obj});
  },

  initMode: function () {
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
    this.data.currentJL =  update(this.data.currentJL,  {$set: {}});
    this.data.zlzg = update(this.data.zlzg, {reljl: {$set: []}});
    this.data.zltg = update(this.data.zltg, {reljl: {$set: []}});
    this.data.aqtg = update(this.data.aqtg, {reljl: {$set: []}});
    this.data.aqjb = update(this.data.aqjb, {reljl: {$set: []}});
    this.data.aqzg = update(this.data.aqzg, {reljl: {$set: []}});
    this.data.yccl = update(this.data.yccl, {reljl: {$set: []}});
    this.data.txmap.set("质量问题整改通知书", this.data.zlzg);
    this.data.txmap.set("安全隐患限期整改通知书", this.data.aqzg);
    this.data.txmap.set("质量局部暂停施工通知书", this.data.zltg);
    this.data.txmap.set("安全隐患责令停工通知书", this.data.aqtg);
    this.data.txmap.set("安全隐患局部停工整改通知书", this.data.aqjb);
    this.data.txmap.set("扬尘处理", this.data.yccl);
  },



    onFetch: function (params, cb) {
    },

    updateComponent: function () {
        this.trigger(this.data);
    },

    getInitialState: function () {
        return this.data;
    }
});
